﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using System.Web.Routing;
using VQWEB.common;
using VQData;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for TagHandler
    /// </summary>
    public class core_TagHandler : IHttpHandler,IRouteHandler
    {
        TagData TagData = new TagData();
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Tag     
            string add_tag = context.Request["add_tag"];
            if (!string.IsNullOrEmpty(add_tag))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                var obj = bussiness.InsertOrUpdate<core_tag>(add_tag);
                context.Response.Write("Thực thi thành công !");
            }
            string view_tag = context.Request["view_tag"];
            if (!string.IsNullOrEmpty(view_tag))
            {
                string Id = context.Request["Id"];
                RenderSingle(context, Convert.ToInt32(Id));
            }
            string list_tag = context.Request["list_tag"];
            if(!string.IsNullOrEmpty(list_tag))
            {
                RenderList(context);
            }
            string delete_tag = context.Request["delete_tag"];
            if (!string.IsNullOrEmpty(delete_tag)) {
                 BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                 bussiness.Delete<core_tag>(delete_tag);
                RenderList(context);
            }
            string lockunlock_tag = context.Request["lockunlock_tag"];
            if (!string.IsNullOrEmpty(lockunlock_tag)) {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.LockUnLock<core_tag>(lockunlock_tag, "tag_active");
                RenderList(context);
            }
            #endregion
        }
        private void RenderSingle(HttpContext context,int Id)
        {
            DataLayer<CoreModelDataContext> layer = new DataLayer<CoreModelDataContext>();
            object obj = layer.Get<core_tag>(
               c => c.tag_id.Equals(Id),
               c => new
               {
                   id = c.tag_id,
                   keyword=c.tag_keyword,
                   hit=c.tag_hit,
                   type = c.tag_type,
                   active = c.tag_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
         private void RenderList(HttpContext context) {

             DataLayer<CoreModelDataContext> layer = new DataLayer<CoreModelDataContext>();
             IList<object> list = layer.List<core_tag>(
                 null,
                 null,
                 null,
                 null,
                c => new
                {
                    id = c.tag_id,
                    keyword = c.tag_keyword,
                    hit = c.tag_hit,
                    type = c.tag_type,
                    active = c.tag_active
                });
             string json = serializer.Serialize(list);
             context.Request.ContentType = "aplication/json";
             context.Response.Write(json);
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}